home *** CD-ROM | disk | FTP | other *** search
Wrap
class CPhitGame extends CMovieClipFresh { var _statReporter; var _arrFieldHosts; var _field = null; var _fieldHost = null; var _doneShuffling = false; var _minDesiredIterations = 100; var _maxDesiredIterations = 1000; var _maxShuffleSlideLength = 5; var _totalIterations = 0; var _bias = 0; var _isFieldSolved = false; var _iLevel = 0; var _iSeedOffset = 0; var _gameWidth = 480; var _gameHeight = 640; function CPhitGame() { super(); } function FirstFrameInitialize() { _root._game = this; if(_root._iStartLevel) { this._iLevel = _root._iStartLevel; } this._statReporter = new CServerStatReporter(); this._arrFieldHosts = new Array(); Key.addListener(this); this.StartFieldSequence(); } function GetLevel() { return this._iLevel; } function GetField() { return this._field; } function GetFieldHost() { return this._fieldHost; } function StartFieldSequence() { FreshDebug.Trace("Seeding"); _root.random.SetSeed(this._iLevel + this._iSeedOffset); this.StartBuildingField(this._iLevel); } function StartBuildingField(iField) { FreshDebug.Trace("StartBuildingField( " + iField + " )"); if(iField == undefined) { iField = this._arrFieldHosts.length; } var _loc3_ = iField; this.attachMovie("Field Host","fieldHost" + _loc3_,_loc3_); this._fieldHost = CFieldHost(this["fieldHost" + _loc3_]); this._arrFieldHosts[iField] = this._fieldHost; this._totalIterations = 0; this._field = null; this._doneShuffling = false; this._isFieldSolved = false; } function onKeyUp() { var _loc2_ = Key.getCode(); FreshDebug.Trace("key up=" + _loc2_); if(Key.isDown(17)) { switch(_loc2_) { case 38: this.FinishCurrentLevel(); break; case 90: this._field.UndoLastPieceMove(); break; case 89: this._field.RedoLastPieceMove(); } } } function IsUndoAvailable() { return !this._isFieldSolved && this._field.IsUndoAvailable(); } function IsRedoAvailable() { return !this._isFieldSolved && this._field.IsRedoAvailable(); } function onEnterFrame() { super.onEnterFrame(); if(!this._fieldHost) { return undefined; } this._field = CPlayingField(this._fieldHost._field); if(this._field == null || !this._field.IsAwake()) { return undefined; } if(this._field && this._field.WantsBuilding()) { this.BuildPendingField(); } if(!this._doneShuffling) { this.ShuffleField(); } else { this.UpdateGame(); } } function UpdateGame() { if(!this._isFieldSolved && this._field.AreAllPiecesWithinTray()) { this.FinishCurrentLevel(); } } function GetFieldConfiguration(iLevel) { if(iLevel < 2) { return new Object({width:4,height:2,minPieceSize:1,maxPieceSize:4,bias:0.5,minTotalIterations:10,maxTotalIterations:50,maxShuffleSlideLength:4}); } if(iLevel < 8) { var _loc2_ = (iLevel - 2) / 6; return new Object({width:6,height:3,minPieceSize:1,maxPieceSize:5,bias:0.5,minTotalIterations:MathUtil.Lerp(50,100,_loc2_),maxTotalIterations:MathUtil.Lerp(100,150,_loc2_),maxShuffleSlideLength:5}); } if(iLevel < 15) { _loc2_ = (iLevel - 8) / 7; return new Object({width:7,height:4,minPieceSize:1,maxPieceSize:6,bias:0.33,minTotalIterations:MathUtil.Lerp(100,200,_loc2_),maxTotalIterations:MathUtil.Lerp(200,300,_loc2_),maxShuffleSlideLength:3}); } if(iLevel < 30) { _loc2_ = (iLevel - 15) / 15; return new Object({width:8,height:5,minPieceSize:1,maxPieceSize:6,bias:0.25,minTotalIterations:MathUtil.Lerp(200,400,_loc2_),maxTotalIterations:MathUtil.Lerp(400,600,_loc2_),maxShuffleSlideLength:2}); } if(iLevel < 50) { _loc2_ = (iLevel - 30) / 20; return new Object({width:9,height:6,minPieceSize:1,maxPieceSize:6,bias:0.25,minTotalIterations:MathUtil.Lerp(400,800,_loc2_),maxTotalIterations:MathUtil.Lerp(800,1200,_loc2_),maxShuffleSlideLength:2}); } if(iLevel < 100) { _loc2_ = (iLevel - 50) / 50; return new Object({width:10,height:6,minPieceSize:2,maxPieceSize:6,bias:0.2,minTotalIterations:MathUtil.Lerp(1000,2000,_loc2_),maxTotalIterations:MathUtil.Lerp(2000,4000,_loc2_),maxShuffleSlideLength:1}); } return new Object({width:12,height:7,minPieceSize:1,maxPieceSize:6,bias:0.2,minTotalIterations:2000,maxTotalIterations:4000,maxShuffleSlideLength:1}); } function BuildPendingField() { FreshDebug.Trace("Trying to build"); var _loc2_ = this.GetFieldConfiguration(this._iLevel); this._field.Build(_loc2_.width,_loc2_.height,_loc2_.minPieceSize,_loc2_.maxPieceSize); this._doneShuffling = false; this._totalIterations = 0; this._bias = _loc2_.bias; this._minDesiredIterations = _loc2_.minTotalIterations; this._maxDesiredIterations = _loc2_.maxTotalIterations; this._maxShuffleSlideLength = _loc2_.maxShuffleSlideLength; this.ScaleField(this._fieldHost); FreshDebug.Assert(!this._field.WantsBuilding(),"!_field.WantsBuilding()"); } function ShufflePerFrame() { var _loc3_ = 1; var _loc2_ = Math.min(8,this._maxDesiredIterations - this._totalIterations); return this._field.Shuffle(_loc3_,_loc2_,this._maxShuffleSlideLength,this._bias); } function ShuffleField() { FreshDebug.Trace("Shuffling"); var _loc3_ = 1; var _loc2_ = Math.min(8,this._maxDesiredIterations - this._totalIterations); this._totalIterations += this.ShufflePerFrame(); if(!this._field.AreAllPiecesOutsideOfTray()) { if(this._totalIterations > this._maxDesiredIterations) { this.StartBuildingField(this._iLevel); } } else if(this._totalIterations >= this._minDesiredIterations) { this._doneShuffling = true; this.OnFinishShuffling(); } } function OnFinishShuffling() { this._field.RememberPieceBasePositions(); this._fieldHost.PlayAppear(); } function OnVictoryDisplayFinished() { FreshDebug.Trace("OnVictoryDisplayFinished"); this._fieldHost.PlayDisappear(); this._fieldHost = null; this._field = null; this.StartNextLevel(); } function FinishCurrentLevel() { FreshDebug.Trace("closing level"); this._isFieldSolved = true; this.ReportLevelStatsToServer(); this._field.PlayVictory(); } function StartNextLevel() { this._iLevel = this._iLevel + 1; FreshDebug.Trace("moving to level " + this._iLevel); _root._presentation.StoreLastPlayedLevel(this._iLevel); FreshDebug.Trace("Fabricating a new level."); this.StartFieldSequence(); } function KillFieldHost(host) { var _loc2_ = 0; while(_loc2_ < this._arrFieldHosts.length) { if(this._arrFieldHosts[_loc2_] == host) { this._arrFieldHosts[_loc2_] = null; break; } _loc2_ = _loc2_ + 1; } } function ScaleField(fieldHost) { var _loc4_ = fieldHost._field._pixelWidth / this._gameWidth; var _loc5_ = fieldHost._field._pixelHeight / this._gameHeight; var _loc3_ = 1 / _loc5_; if(_loc4_ > _loc5_) { _loc3_ = 1 / _loc4_; } fieldHost._xscale = fieldHost._yscale = _loc3_ * 100; fieldHost._x = (this._gameWidth - fieldHost._field._pixelWidth * _loc3_) * 0.5; fieldHost._y = fieldHost._field._tableauPixelHeight * _loc3_; fieldHost._field.SizeFrame(_loc3_); } function ReportLevelStatsToServer() { this._statReporter.ReportLevelStats(this._iLevel,this._field.GetTimeTaken(),this._field.GetNumMovesMade()); } }